//
//  XXSkin.m
//  dyss_v2
//
//  Created by LXF on 16/3/2.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "XXSkin.h"

#import <CustomLogTools.h>
#import "UIColor+customHEX.h"

@implementation XXSkin

@dynamic absPath;

-(UIColor *)showColor{
    NSString *result = self.showColorHexString;
    
    NSArray *allColor = [result componentsSeparatedByString:@","];
    
    CGFloat alpha = [allColor[0] doubleValue]/100;

    NSString *hex = allColor[1];
    
    return [UIColor colorWithHexString:hex alpha:alpha];
}

-(NSString *)absPath{
    if (self.isDownloadSkin) {
        return [self generatePath:self.filePath];
    }
    return self.filePath;
}

- (NSString *)generatePath:(NSString *)filePath {
    if (filePath == nil || filePath.length == 0) {
        NSAssert(NO, @"网络下载的皮肤信息,地址有误..");
        return nil;
    }
    NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *path = [NSString stringWithFormat:@"%@/%@", cache, filePath];
    return path;
}

+(NSArray *)skinArray{
    NSArray *array = [self allDbObjects];
    if (array != nil || array.count > 0) {
        return array;
    }
    
    NSString *file = [[NSBundle mainBundle]pathForResource:@"DefaultSkinList.plist" ofType:@""];
    array = [NSArray arrayWithContentsOfFile:file];
    
    array = [self modelsWithDicts:array];
    
    [self save:array];
    
    return array;
}

+(void)save:(NSArray<BaseModel *> *)models{
    [[STDbQueue defaultQueue]execute:^(STDb *db) {
        for (BaseModel *model in models) {
            if ([model replaceToDb:db] == false) {
                printE(@"插入数据失败");
            }
        }
    }];
}

@end
